package com.opengltest;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

/**
* @author oven:
* @version 创建时间：2013-1-25 下午4:07:06
* 
*/
public class ColorSquare extends Square{
	
	// The colors mapped to the vertices.
	float[] colors = {
	 1f, 0f, 0f, 1f, // vertex 0 red
	 0f, 1f, 0f, 1f, // vertex 1 green
	 0f, 0f, 1f, 1f, // vertex 2 blue
	 1f, 0f, 1f, 1f, // vertex 3 magenta
	};
	
	FloatBuffer colorBuffer;
	
	
	public ColorSquare(){
		// float has 4 bytes, colors (RGBA) * 4 bytes
		ByteBuffer cbb= ByteBuffer.allocateDirect(colors.length * 4);
		cbb.order(ByteOrder.nativeOrder());
		colorBuffer = cbb.asFloatBuffer();
		colorBuffer.put(colors);
		colorBuffer.position(0);
	}

	@Override
	public void draw(GL10 gl) {
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		  
		//启用颜色buff传入
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		// 给不同顶点设定颜色值  顺序与顶点顺序一致
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
		  
		super.draw(gl);
		// 关闭颜色buff
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	}
}

